package br.org.acessobrasil.portal.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtils {
	public static String substituiAtributoTag(String html, String tag, String atributo, String novoValor) {
		String retorno = "";
		String reg = "<" + tag + "(\\s|\\s.*?\\s)" + atributo + "=\".*?\"(.*?)>";
		Pattern pat = Pattern.compile(reg, Pattern.DOTALL);
		Matcher mat = pat.matcher(html);
		if (mat.find()) {
			String ini = mat.group(1);
			String fim = mat.group(2);
			retorno = mat.replaceAll("<" + tag + ini + atributo + "=\"" + novoValor + "\" " + fim + ">");
		}
		return retorno;
	}
	public static String getConteudoTexto(String conteudo){
		//System.out.println("getConteudoCego("+conteudo+")");
		
		Pattern patTag = Pattern.compile("<.*?>",Pattern.DOTALL);
		Matcher matTag = patTag.matcher(conteudo);
		if(matTag.find()){			
			conteudo = matTag.replaceAll(" ");
		}	
		
		patTag = Pattern.compile("\\s\\s\\s*",Pattern.DOTALL);
		matTag = patTag.matcher(conteudo);
		
		if(matTag.find()){			
			conteudo=matTag.replaceAll(" ");
		}	
		
		return conteudo;
	}
	
	/**
	 * Coloca uma tag &lt;base href="http://www.seudominio.com" &gt;
	 * no &lt;head&gt; depois do &lt;/title&gt;, se houver.<br>
	 * Caso nao haja a tag de titulo coloca depois da tag head 
	 * @param baseHref eg.: http://www.seudominio.com/pasta-x/pagina-y.html
	 * @param html original
	 * @return html alterado
	 * @throws Exception
	 */
	public static String setBaseHref(String baseHref, String html) throws Exception{
		if(html.contains("</title>")){
			return html.replace("</title>", "</title>\n\t<base href=\""+baseHref+"\" />");
		}else if(html.contains("</TITLE>")){//html 4?
			return html.replace("</TITLE>", "</TITLE>\n\t<BASE HREF=\""+baseHref+"\">");
		}else if(html.contains("<head>")){
			return html.replace("<head>", "<head>\n\t<base href=\""+baseHref+"\" />");
		}else if(html.contains("<HEAD>")){
			return html.replace("<HEAD>", "<HEAD>\n\t<base href=\""+baseHref+"\" />");
		}else{
			throw new Exception("O documento nao possui a tag head");
		}
	}
}
